<?php
/**
 * IndexController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Install_IndexController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */
class Install_IndexController extends Madoqua_Controller_Action_Abstract {

    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_modelLoader->load('BlogAdmin_Config');
        //load the config model
    }

    /**
     * let's install this dikdik!
     */
    public function indexAction()
    {
        if (empty($_SERVER['MADOQUAHOST']) || empty($_SERVER['REWRITEBASE'])) {
            throw new Exception('Madoqua host param not in $_SERVER');
        } else {
            $this->_config->url = $_SERVER['MADOQUAHOST'];
            //base url
            $this->_config->rewriting->base = $_SERVER['REWRITEBASE'];
            //rewrite base

            BlogAdmin_Config::saveApplicationConfig();
            //save the new config
        }
        //set up config file
        
        $this->_forward('start');

    }
    
    /**
     * start the installation
     *
     */
    public function startAction()
    {
        
        $form = $this->_formLoader->get();
        $this->view->form = $form;
        //form
        
        $form->setDefaults(array(
            'name' => trim($this->_config->name),
            'locale' => $this->_config->datetime->locale
        ));
        //default form values
        
        if ($form->isPrg() && $form->isValid($form->getPrgData())) {
        
            $this->_config->name = $form->getValue('name');
            //name
            $this->_config->datetime->locale = $form->getValue('locale');
            //locale
            
            BlogAdmin_Config::saveApplicationConfig();
            //save the configuration
            
            $options = array(
                'module' => 'install',
                'controller' => 'database',
                'action' => 'index'
            );
            $this->getHelper('redirector')->gotoRouteAndExit($options, 'default', true);
            //redirect to database setup    
        }
        
        
    }

    /**
     * finish the installation
     */
    public function finishAction()
    {
        touch(Madoqua_Application::getApplicationDirectory() . DIRECTORY_SEPARATOR  . '/config/installed');
        //create a file to track installation status
    }

}